package com.mark.web.data.util;

import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Description: 分页工具类
 * @Author: Mark
 * @CreateDate: 2020/5/23 20:19
 * @Copyright : 豆浆油条个人非正式工作室
 */
public class PageInfoUtil {

    private PageInfoUtil() {

    }

    /**
     * VO信息的分页
     *
     * @param pojoList 原始的列表
     * @param mapper   pojo和vo转换的方法
     * @param <V>      Vo
     * @param <P>      Pojo
     * @return PageInfo<V>
     */
    public static <V, P> PageInfo<V> build(List<P> pojoList, Function<P, V> mapper) {
        if (CollectionUtils.isEmpty(pojoList)) {
            return new PageInfo<>();
        }
        //构建返回分页VO
        List<V> properNounVOList = pojoList
                .stream()
                .map(mapper)
                .collect(Collectors.toList());
        PageInfo<V> result = new PageInfo<>(properNounVOList);
        result.setTotal(new PageInfo<>(pojoList).getTotal());
        return result;
    }
}
